import java.util.Random;
import java.util.concurrent.BlockingQueue;

class Add{
    int count = 0;
    public void add() {
        synchronized(this) {
            count++;
        }
    }
}
public class Text {
    public static  int sum2 = 0;
    public static  int sum1 = 0;
    public static void main1(String[] args) throws InterruptedException {
        int[] array = new int[10000000];
        Random rd = new Random();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < array.length; i++) {
            array[i] = rd.nextInt(100 - 1 + 1) + 1;
        }
        Thread t1 = new Thread(() -> {
            for (int i = 1; i < array.length; i += 2) {
                sum1 += array[i];
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < array.length; i += 2) {
                sum2 += array[i];
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long endTime = System.currentTimeMillis();
        System.out.println("结果 :" + (sum1 + sum2));
        System.out.println("时间 :" + (endTime - startTime));
    }
    volatile int a = 0;
    public static void main2(String[] args) throws InterruptedException {
        Object key= new Object();
        Add a = new Add();
        Thread t1 = new Thread(() ->{
            for(int i = 0; i< 10000;i++) {
                a.add();
            }
        });
        Thread t2 = new Thread(() ->{
            for(int i = 0; i< 10000;i++) {
                a.add();

            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(a.count);
    }

    public static void main(String[] args) {
    }
}
